package pl.edu.mimuw.irs.core.util;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import pl.edu.mimuw.irs.core.model.design.IRSActivity;
import pl.edu.mimuw.irs.core.model.design.IRSArc;
import pl.edu.mimuw.irs.core.model.design.IRSNodeInterface;
import pl.edu.mimuw.irs.core.model.design.IRSPlace;
import pl.edu.mimuw.irs.core.model.design.IRSRequirement;
import pl.edu.mimuw.irs.core.model.design.IRSTransition;
import pl.edu.mimuw.irs.core.model.design.IRSWorkflow;
import pl.edu.mimuw.irs.core.model.design.exception.InternalIRSException;
import pl.edu.mimuw.irs.core.model.design.exception.InternalIRSException.Cause;
import pl.edu.mimuw.irs.core.model.petrinet.Arc;
import pl.edu.mimuw.irs.core.model.petrinet.INode;
import pl.edu.mimuw.irs.core.model.petrinet.Net;
import pl.edu.mimuw.irs.core.model.petrinet.Place;
import pl.edu.mimuw.irs.core.model.petrinet.Transition;

/**
 * @author Jakub Rauch
 * Created on: 2009-05-24
 */
public class Converter {

	// *** Static methods
	
	public static IRSArc asIRSArc(Arc arc) {
		if (!(arc instanceof IRSArc)) 
			throw new InternalIRSException(Cause.NOT_IRS_ARC, arc.toString());
		return (IRSArc)arc;
	}

	public static IRSNodeInterface asIRSNodeInterface(INode node) {
		if (!(node instanceof IRSNodeInterface)) 
			throw new InternalIRSException(Cause.NOT_IRS_NODE, node.toString());
		return (IRSNodeInterface)node;
	}
	
	public static IRSPlace asIRSPlace(Place place) {
		if (!(place instanceof IRSPlace)) 
			throw new InternalIRSException(Cause.NOT_IRS_PLACE, place.toString());
		return (IRSPlace)place;
	}

	public static IRSRequirement asIRSRequirement(INode node) {
		if (!(node instanceof IRSRequirement)) 
			throw new InternalIRSException(Cause.NOT_IRS_REQUIREMENT, node.toString());
		return (IRSRequirement)node;
	}

	public static IRSActivity asIRSActivity(IRSNodeInterface node) {
		if (!(node instanceof IRSActivity)) 
			throw new InternalIRSException(Cause.NOT_IRS_ACTIVITY, node.toString());
		return (IRSActivity)node;
	}

	public static IRSPlace asIRSPlace(IRSNodeInterface place) {
		if (!(place instanceof IRSPlace)) 
			throw new InternalIRSException(Cause.NOT_IRS_PLACE, place.toString());
		return (IRSPlace)place;
	}
	
	public static IRSTransition asIRSTransition(Transition transition) {
		if (!(transition instanceof IRSTransition)) 
			throw new InternalIRSException(Cause.NOT_IRS_TRANSITION, transition.toString());
		return (IRSTransition)transition;
	}
	
	public static IRSTransition asIRSTransition(IRSNodeInterface transition) {
		if (!(transition instanceof IRSTransition)) 
			throw new InternalIRSException(Cause.NOT_IRS_TRANSITION, transition.toString());
		return (IRSTransition)transition;
	}

	public static IRSWorkflow asIRSWorkflow(Net net) {
		if (!(net instanceof IRSWorkflow)) 
			throw new InternalIRSException(Cause.NOT_IRS_WORKFLOW, net.toString());
		return (IRSWorkflow)net;
	}
	
	public static <T> Map<T, Integer> listToMap(List<T> list) {
		Map<T, Integer> map = new LinkedHashMap<T, Integer>();
		for (T element : list) {
			Integer count = map.get(element);
			if (count == null) count = 0;
			map.put(element, count + 1);
		}
		return map;
	}
}
